package main

import (
	"errors"
	"fmt"
	"time"
	"unsafe"
)

/*
*
字符串常用的函数
看到:202页
*/
func main() {
	fmt.Println("Hello, World!name:")
	fmt.Println("==获取当前时间,返回值是一个时间对象==")
	var nowVal = time.Now()
	fmt.Printf("nowVal:%v,类型为:%T,字节大小:%v\n", nowVal, nowVal, unsafe.Sizeof(nowVal))

	fmt.Println("==获取时间的各个属性")
	var yearVal = nowVal.Year()
	fmt.Printf("yearVal:%v,类型为:%T,字节大小:%v\n", yearVal, yearVal, unsafe.Sizeof(yearVal))
	var monthVal = int(nowVal.Month())
	fmt.Printf("monthVal:%v,类型为:%T,字节大小:%v\n", monthVal, monthVal, unsafe.Sizeof(monthVal))
	var dayVal = nowVal.Day()
	fmt.Printf("dayVal:%v,类型为:%T,字节大小:%v\n", dayVal, dayVal, unsafe.Sizeof(dayVal))
	var hourVal = nowVal.Hour()
	fmt.Printf("hourVal:%v,类型为:%T,字节大小:%v\n", hourVal, hourVal, unsafe.Sizeof(hourVal))
	var minuteVal = nowVal.Minute()
	fmt.Printf("minuteVal:%v,类型为:%T,字节大小:%v\n", minuteVal, minuteVal, unsafe.Sizeof(minuteVal))
	var secondVal = nowVal.Second()
	fmt.Printf("secondVal:%v,类型为:%T,字节大小:%v\n", secondVal, secondVal, unsafe.Sizeof(secondVal))

	fmt.Println("==获取时间的年月日时分秒")
	var yearMonthDayVal = nowVal.Format("2006-01-02 15:04:05")
	fmt.Printf("yearMonthDayVal:%v,类型为:%T,字节大小:%v\n", yearMonthDayVal, yearMonthDayVal, unsafe.Sizeof(yearMonthDayVal))

	/* 睡眠 */
	time.Sleep(time.Second * 2)
	fmt.Sprintf("当前时间是:%d-%d-%d %d:%d:%d", yearVal, monthVal, dayVal, hourVal, minuteVal, secondVal)

	fmt.Println("==获取时间戳==")
	var timeStampVal = nowVal.Unix()
	fmt.Printf("timeStampVal:%v,类型为:%T,字节大小:%v\n", timeStampVal, timeStampVal, unsafe.Sizeof(timeStampVal))
	fmt.Println("==获取时间戳纳秒数==")
	var timeStampMilliVal = nowVal.UnixNano()
	fmt.Printf("timeStampMilliVal:%v,类型为:%T,字节大小:%v\n", timeStampMilliVal, timeStampMilliVal, unsafe.Sizeof(timeStampMilliVal))

	fmt.Println("==内置函数==")
	var num2 = new(int)
	*num2 = 100
	fmt.Printf("num2:%v,类型为:%T,字节大小:%v,地址:%v,指向的值为:%v\n", num2, num2, unsafe.Sizeof(num2), &num2, *num2)

	fmt.Println("==自定义函数(测试异常)==")
	var res = testFunc(10, 0)
	fmt.Printf("res:%v,类型为:%T,字节大小:%v\n", res, res, unsafe.Sizeof(res))
	var err = testDefError("config1.ini")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("==代码继续执行==")
}

func testDefError(name string) (err error) {
	if name == "config.ini" {
		return nil
	} else {
		return errors.New("文件不存在")
	}
}

/*
演示异常的处理方法
*/
func testFunc(a int, b int) int {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("捕获到异常:", err)
		}
	}()

	var res = a / b
	return res
}
